package org.apache.osgimaker.analyse.algorithm.renderer;

import org.apache.osgimaker.analyse.algorithm.ClassAttributes;
import org.apache.osgimaker.analyse.algorithm.graph.AtomicVertex;
import org.apache.osgimaker.analyse.algorithm.graph.StrongComponent;


/**
 * Plain text renderer of an {@link AtomicVertex} with 
 * {@link ClassAttributes}.
 * 
 */
public class PlainClassRenderer implements AtomicVertexRenderer 
{
  /**
   * Renderes the specified vertex. It is assumed that the vertex attributes
   * are of the type {@link org.apache.osgimaker.analyse.algorithm.ClassAttributes}.
   * @param vertex Vertex to be rendered.
   * @return the rendered vertex.
   */
  public String render(AtomicVertex vertex, StrongComponent cycle, 
                       int layerIndex) 
  {
    if (vertex.getAttributes() instanceof ClassAttributes) 
    {
      int usesInternal = 0;
      int usesExternal = 0;
      for (int i = 0, n = vertex.getNumberOfOutgoingArcs(); i < n; i++) 
      {
        if (((AtomicVertex) vertex.getHeadVertex(i)).isGraphVertex()) 
        {
          usesInternal++;
        } else 
        {
          usesExternal++;
        }
      }
      StringBuffer result 
          = new StringBuffer(vertex.getAttributes().toString());
      result.append(": Used by ").append(vertex.getNumberOfIncomingArcs())
            .append(" classes. Uses ").append(usesInternal).append('/')
            .append(usesExternal).append(" internal/external classes");
      return new String(result);
    } 
    throw new IllegalArgumentException(
                  "Missing class attributes in vertex " + vertex);
  }
} //class